package com.ourhours.message.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ourhours.link.annotation.Permission;
import com.ourhours.link.dto.UserInfoDto;
import com.ourhours.message.common.BaseResult;
import com.ourhours.message.domain.EmailSend;
import com.ourhours.message.service.EmailSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 邮件发送记录表 前端控制器
 * </p>
 *
 * @author dalong.wang
 * @since 2018-03-21
 */
@Controller
@Slf4j
@RequestMapping("/emailSend")
public class EmailSendController extends BaseController {

    @Autowired
    EmailSendService emailSendService;

    @Permission("messageEmailSend")
    @GetMapping("/list")
    public String list() {
        return "emailSend/list";
    }

    @Permission("messageEmailSendList")
    @PostMapping("/listPage")
    @ResponseBody
    public BaseResult listPage(EmailSend entity, Integer offset, Integer limit) {
        try {
            UserInfoDto user = getLoginUser();
            entity.setDeleted(0L);
            if (user.getVendorId() != null && user.getVendorId() != 0L) {
                entity.setVendorId(user.getVendorId());
            }
            EntityWrapper<EmailSend> wrapper = new EntityWrapper<EmailSend>(entity);
            wrapper.orderBy("id",false);
            Page<EmailSend> page0 = new Page<>(offset / limit + 1, limit);
            Page<EmailSend> page = emailSendService.selectPage(page0, wrapper);
            log.info("==> EmailSendController.listPage查询邮件记录列表成功,结果:{}", page);
            return BaseResult.success(page);
        } catch (Exception e) {
            log.error("==> EmailSendController.listPage查询邮件记录列表失败，异常{}", e);
            return BaseResult.failure("查询失败!");
        }
    }
}